<?php

class spPageNew extends spPage
{
	private $data;
	
	public function setData( $data )
	{
		$this->data = $data;
	}
	
	public function render()
	{
		$d = $this->data;
		
		parent::spRenderDefaults();
		parent::spRenderHeading( $d["heading"], isset($d["headingimage"])?$d["headingimage"]:null );
		
		$formBuilder = $d["formbuilder"];
		$formBuilder->setDatabase( $d["database"] );
		
		$formBuilder->setFormButtonsBottom( new spButtons(array(
			new spButton(array( 
				"class"		=> "spAjaxForm",
				"title" 	=> "Save",
				"name"		=> $d["ajax"]
			))		
		)));	
		
		parent::addChild( $formBuilder );
	
		parent::render();
	}
}

function spAjaxNew( $database, $redirect )
{
	$database->updateFromRequest();
	if ( !$database->save() )
	{
		$errors = $database->getFormErrors();
		$json = array(
			"formerrors" => $errors
		);
		echo json_encode( $json );
	}
	else
	{
		echo '{"redirect":"'.$redirect.'"}';
	}	
}

?>